package bd.project;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 *
 * @author jhonlp
 */
public class Login extends JFrame implements ActionListener {

    private JButton btLogin, btExit;
    private JTextField fUser;
    private JPasswordField fPassword;
    private JPanel mainPanel;
    private String user, password;
    private JLabel lUser, lPassword, backG;
    private InterfazGrafica interfaz = new InterfazGrafica();

    public Login() {
        super("Project BD - Login");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
      
        inicializarComponentes();
        acomodarComponentes();
        conectarEscuchas();

        Container contenedor = getContentPane();
        contenedor.setLayout(null);
        setSize(450, 500);
        setVisible(true);
        setResizable(false);

        contenedor.add(mainPanel);

    }

    public void keyPressed(KeyEvent evento) {

        JOptionPane.showMessageDialog(this, "Se oprimió tecla:" + evento.getKeyText(evento.getKeyCode()));
    }

    public void actionPerformed(ActionEvent evento) {

        if (evento.getSource() == btExit) {
            this.dispose();
        }
        if (evento.getSource() == btLogin) {

            user = fUser.getText();
            password = fPassword.getText();
            if (user.equals("") || password.equals("")) {
                JOptionPane.showMessageDialog(this, " el campo contraseña o password esta vacio intente con uno valido");
            }
            String s = interfaz.consultarUsuario(user);

            if (password.equals(s)) {
                fUser.setText("");
                fPassword.setText("");
                this.dispose();
                Administrador objAdministrador = new Administrador();
                objAdministrador.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            } else {
                JOptionPane.showMessageDialog(this, " el campo contraseña o password no son validos ");

                fUser.setText("");
                fPassword.setText("");


            }
        }
    }

    public void inicializarComponentes() {

        ImageIcon image = new ImageIcon("colegio.jpg");

        backG = new JLabel();

        backG.setIcon(image);

        lUser = new JLabel("Usuario");
        lPassword = new JLabel("Password");

        fUser = new JTextField();
        fPassword = new JPasswordField();

        btExit = new JButton("Salir");
        btLogin = new JButton("Login");

        mainPanel = new JPanel(null);
        mainPanel.setBackground(Color.WHITE);
        mainPanel.add(lUser);
        mainPanel.add(fUser);
        mainPanel.add(lPassword);
        mainPanel.add(fPassword);
        mainPanel.add(btExit);
        mainPanel.add(btLogin);
        mainPanel.add(backG);

    }

    public void acomodarComponentes() {

        lUser.setBounds(100, 310, 130, 30);
        fUser.setBounds(230, 310, 130, 30);
        lPassword.setBounds(100, 350, 130, 30);
        fPassword.setBounds(230, 350, 130, 30);
        btExit.setBounds(100, 390, 130, 30);
        btLogin.setBounds(230, 390, 130, 30);
        mainPanel.setBounds(0, 0, 449, 499);
        backG.setBounds(0, 0, 449, 499);

    }

    public void conectarEscuchas() {
        btExit.addActionListener(this);
        btLogin.addActionListener(this);
    }

    public static void main(String args[]) {
        Login application = new Login();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
